www.gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\datasets\img2xi.m

    function [X,I,K,width,height] = img2xi(root,from,to,format)
% IMG2XI loads images from files and reshape them onto vectors.
%   [X,I,K,width,height] = img2xi(root,from,to,format)
%
% IMG2XI loads images of given format (see help imread) from 
%  directories numbered by numbers from - to. These directories 
%  are supposed to be in given root directory. Loaded images
%  are reshaped to the vectors and normalized to <0,1>. 
%  The matrix X of loaded images and vector I of labels 
%  determined according to number of directory is returned.
%
%  Input:
%  root [string] name of the direcory where the numbered
%          direcories are placed.
%  from [1x1] number coresponding to the first class - name
%          of directory.
%  to [1x1] number coresponding to the last class - name
%          of directory.
%
% Output:
%  X [DxN] loaded images stored as N vectors of dimension D.
%  I [1xN] class labels of the vectors X which correspond to
%          the direcotry names (numbers).
%  K [1xC] contains numbers of vectors in each class. The C is
%          number of classes.
%  width [1x1] image width.
%  height [1x1] image height.
%
% See also IMG2X, X2IMG, PGM2XI, X2PGM, PGM2X.

% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz
% Written Vojtech Franc (diploma thesis) 02.01.2000
% Modifications
% 16-apr-2001, V.Franc, created.
%

X=[];
I=[];
K=zeros(1,to-from+1);

for j=from:to,
  d = dir([root '/' num2str(j)]);
  for i=1:length(d),
    if ~d(i).isdir,
      
      img = imread([root '/' num2str(j) '/' d(i).name],format);
      img = im2double( img );
      img = img/max(max(img));
      
      width=size(img,2);
      height=size(img,1);
      
      I= [I,j];
      X= [X,reshape(img',size(img,1)*size(img,2),1)];
      K(j-from+1)=K(j-from+1)+1;
    end
  end
end

return;